<?php
/**
 * User: t.young
 * Date: 2017/12/18
 * Time: 14:21
 */

namespace backend\modules\system\controllers;


use backend\component\BackendBaseController;
use backend\service\AdminService;
use common\component\ClassFactory;
use common\models\permis\Role;

class RoleController extends BackendBaseController
{
    public function actions()
    {
        return [
            "modify" => [
                'class' => "backend\actions\ModifyAction",
                'modelName'=> Role::className()
            ],
            "delete" =>[
                'class' => "backend\actions\DeleteAction",
                'modelName'=>Role::className(),
                'is_physics' => true
            ],
        ];
    }

    public function actionList() {
        if (\Yii::$app->request->getIsAjax()) {
            return AdminService::roleListData(\Yii::$app->request->post());
        }
        $model = ClassFactory::getInstance(Role::className());
        return $this->render("list",["model"=>$model]);
    }


    public function actionEdit() {
        $id = \Yii::$app->request->get("id",0);
        $model = Role::modelById($id);
        $roleTree = AdminService::roleTree();
        return $this->render("edit",['model'=>$model,"roleTree"=>$roleTree]);
    }

    public function actionMenu() {
        if (\Yii::$app->request->getIsAjax()) {
            $role_id = \Yii::$app->request->get("id");
            $task_ids = \Yii::$app->request->post("taskIds","");
            return AdminService::modifyRoleTask($role_id,$task_ids);
        }
        $role_id = \Yii::$app->request->get("id");
        $model = Role::modelById($role_id);
        $tasks = AdminService::menuListByRoleId($role_id);
        return $this->render("menu",["model"=>$model,"task"=>json_encode($tasks)]);
    }



}